package com.example.yml0528.Service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.example.yml0528.LoginActivity;
import com.example.yml0528.MainActivity;
import com.example.yml0528.User;

public class LoginService extends Service {
    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        String name = intent.getExtras().getString("name");
        String pass = intent.getExtras().getString("pass");
        User user = (User) intent.getExtras().get("user");
        Intent intent1 = new Intent();
        boolean nonnull = nonnull(name, pass);
        if (nonnull) {
            if (name.equals(user.getUsername())) {
                if (name.equals(user.getUsername()) && pass.equals(user.getPw())) {
                    intent1.putExtra("flag", true);
                    intent1.putExtra("msg", "登录成功");
                    intent1.putExtra("gbuser", user);
                    sendBroadcast(intent1);
                } else {
                    intent1.putExtra("flag", false);
                    intent1.putExtra("msg", "用户名或密码错误");
                    sendBroadcast(intent1);
                }
            } else {
                intent1.putExtra("flag", false);
                intent1.putExtra("msg", "该用户未注册");
                sendBroadcast(intent1);
            }
        }
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    public boolean nonnull(String uname, String upw) {
        if (TextUtils.isEmpty(uname) || TextUtils.isEmpty(upw)) {
            Toast.makeText(getApplicationContext(), "用户名或密码不能为空", Toast.LENGTH_SHORT).show();
            return false;
        } else {
            if (uname.length() < 5 || upw.length() < 6) {
                Toast.makeText(getApplicationContext(), "用户名或密码最少6位", Toast.LENGTH_SHORT).show();
                return false;
            } else {
                return true;
            }
        }
    }

}
